package org.jeecg.modules.bj.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.entity.ActZprocess;
import org.jeecg.modules.activiti.service.Impl.ActBusinessServiceImpl;
import org.jeecg.modules.activiti.service.Impl.ActZprocessServiceImpl;
import org.jeecg.modules.bj.dto.BjBdBean;
import org.jeecg.modules.bj.dto.BjQuitBean;
import org.jeecg.modules.bj.dto.ToMyApplyInput;
import org.jeecg.modules.bj.entity.*;
import org.jeecg.modules.bj.mapper.*;
import org.jeecg.modules.bj.service.IBjIdcardfkService;
import org.jeecg.modules.bj.service.IYgdabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: bj_idcardfk
 * @Author: jeecg-boot
 * @Date:   2021-10-22
 * @Version: V1.0
 */
@Service
@Transactional
public class BjIdcardfkServiceImpl extends ServiceImpl<BjIdcardfkMapper, BjIdcardfk> implements IBjIdcardfkService {
    @Resource
    BjIdcardfkMapper bjIdcardfkMapper;
    @Resource
    BjYgruzhiMapper bjYgruzhiMapper;
    @Resource
    BjQuitMapper bjQuitMapper;
    @Resource
    BjGwbdMapper bjGwbdMapper;
    @Autowired
    ActBusinessServiceImpl actBusinessService;
    @Autowired
    private ActZprocessServiceImpl actZprocessService;
    @Autowired
    YgdabServiceImpl ygdabService;

    @Override
    public List<Map<String,String>> addRuzhi(List<Map> dataList) {
        //存储入职信息
        List<Map<String,String>> list=new ArrayList<>();
        for(int i=0;i<dataList.size();i++){
            Map dataMap=(Map)dataList.get(i);
            String ygid=dataMap.get("id").toString();
            String name=dataMap.get("name").toString();
            String sex=dataMap.get("sex").toString();
            String zidanwei=dataMap.get("zidanwei").toString();
            String zyreason=dataMap.get("zyreason").toString();
            String sfzh=dataMap.get("sfzh").toString();
            String zydate=dataMap.get("zydate").toString().substring(0,10);
            String qxdate=dataMap.get("qxdate").toString().substring(0,10);
            String remark=dataMap.get("remark")==null?"":dataMap.get("remark").toString();
            String gwname=dataMap.get("gwname")==null?"":dataMap.get("gwname").toString();
            String ruzhiid=dataMap.get("ruzhiid")==null?"":dataMap.get("ruzhiid").toString();
            if(StringUtils.isNotEmpty(ruzhiid)){
                BjYgruzhi bjYgruzhi=bjYgruzhiMapper.selectById(ruzhiid);
                bjYgruzhi.setZidanwei(zidanwei);
                bjYgruzhi.setZyreason(zyreason);
                bjYgruzhi.setZydate(zydate);
                bjYgruzhi.setQxdate(qxdate);
                bjYgruzhi.setRemark(remark);
                bjYgruzhiMapper.updateById(bjYgruzhi);
                Map<String,String> map1=new HashMap<>();
                map1.put("ruzhiid",ruzhiid);
                map1.put("sfzh",sfzh);
                list.add(map1);
            }else{
                BjYgruzhi bjYgruzhi=new BjYgruzhi();
                bjYgruzhi.setYgid(ygid);
                bjYgruzhi.setName(name);
                bjYgruzhi.setSex(sex);
                bjYgruzhi.setGwname(gwname);
                bjYgruzhi.setSfzh(sfzh);
                bjYgruzhi.setZidanwei(zidanwei);
                bjYgruzhi.setZyreason(zyreason);
                bjYgruzhi.setZydate(zydate);
                bjYgruzhi.setQxdate(qxdate);
                bjYgruzhi.setRemark(remark);
                bjYgruzhiMapper.insert(bjYgruzhi);
                Map<String,String> map1=new HashMap<>();
                map1.put("ruzhiid",bjYgruzhi.getId());
                map1.put("sfzh",sfzh);
                list.add(map1);
            }
        }
        return list;
    }

    @Override
    public ActBusiness saveActBusinessBeforeApprove(String ids,String type){
        String name="";
        BjIdcardfk bjIdcardfk=new BjIdcardfk();
        if(type.equals("1")){
            name="员工入职申请";
            bjIdcardfk.setRuzhiJson(ids);
        }
        if(type.equals("2")){
            name="员工离职申请";
            bjIdcardfk.setLizhiJson(ids);
        }
        if(type.equals("3")){
            name="员工调动变动申请";
            bjIdcardfk.setBiandongJson(ids);
        }
        save(bjIdcardfk);

        //添加入职流程申请
        LambdaQueryWrapper<ActZprocess> wrapper = new LambdaQueryWrapper<ActZprocess>();
        wrapper.orderByAsc(ActZprocess::getProcessKey).orderByDesc(ActZprocess::getVersion);
        wrapper.eq(ActZprocess::getLatest, 1);
        wrapper.eq(ActZprocess::getName, name);
        ActZprocess actZprocess = actZprocessService.getOne(wrapper);
        String tableName = "bj_idcardfk";
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String username = sysUser.getUsername();
        ActBusiness actBusiness = new ActBusiness();
        actBusiness.setUserId(username);
        actBusiness.setTableId(bjIdcardfk.getId());
        actBusiness.setProcDefId(actZprocess.getId());
        actBusiness.setTitle(name);
        actBusiness.setTableName(tableName);
        actBusinessService.save(actBusiness);
        return  actBusiness;
    }

    @Override
    public String deleteActBusinessById(String actbussinessid){
        actBusinessService.removeById(actbussinessid) ;
        return "sucess";
    }

    @Override
    public List<Map<String,String>> addLizhi(List<BjQuitBean> bjQuitBeanList) {
        List<Map<String,String>> list=new ArrayList<>();
        for(BjQuitBean bjQuitBean:bjQuitBeanList){
            if(StringUtils.isNotEmpty(bjQuitBean.getLizhiid())){
                BjQuit bjQuit=bjQuitMapper.selectById(bjQuitBean.getLizhiid());
                bjQuit.setJyreason(bjQuitBean.getJyreason());
                bjQuit.setJydate(bjQuitBean.getJydate().substring(0,10));
                bjQuit.setTxdate(bjQuitBean.getTxdate().substring(0,10));
                bjQuit.setRemark(bjQuitBean.getRemark());
                bjQuit.setXwdate(bjQuitBean.getXwdate().substring(0,10));
                bjQuit.setLizhireason(bjQuitBean.getLizhireason());
                bjQuit.setLastworkdate(bjQuitBean.getLastworkdate().substring(0,10));
                bjQuit.setMtjilu(bjQuitBean.getMtjilu());
                bjQuit.setJzdate(bjQuitBean.getJzdate().substring(0,10));
                bjQuit.setImage1(bjQuitBean.getImage1());
                bjQuit.setImage2(bjQuitBean.getImage2());
                bjQuit.setImage3(bjQuitBean.getImage3());
                bjQuitMapper.updateById(bjQuit);
                Map<String,String> map1=new HashMap<>();
                map1.put("lizhiid",bjQuitBean.getLizhiid());
                map1.put("sfzh",bjQuitBean.getSfzh());
                list.add(map1);
            }else{
                BjQuit bjQuit=new BjQuit();
                bjQuit.setYgid(bjQuitBean.getId());
                bjQuit.setName(bjQuitBean.getName());
                bjQuit.setSex(bjQuitBean.getSex());
                bjQuit.setSfzh(bjQuitBean.getSfzh());
                bjQuit.setGwname(bjQuitBean.getGwname());
                bjQuit.setBumen(bjQuitBean.getBmname());
                bjQuit.setZhiwu(bjQuitBean.getZwgz());
                List<BjYgruzhi> bjYgruzhiList=bjYgruzhiMapper.getListByYgid(bjQuitBean.getId());
                if(bjYgruzhiList.isEmpty()==false){
                    bjQuit.setRzriqi(new SimpleDateFormat("yyyy-MM-dd").format(bjYgruzhiList.get(0).getCreateTime()));
                    bjQuit.setZdwname(bjYgruzhiList.get(0).getZidanwei());
                }else{
                    bjQuit.setRzriqi(StringUtils.isNotEmpty(bjQuitBean.getHtstdt())?bjQuitBean.getHtstdt():"");
                    bjQuit.setZdwname("");
                }
                bjQuit.setJyreason(bjQuitBean.getJyreason());
                bjQuit.setJydate(bjQuitBean.getJydate().substring(0,10));
                bjQuit.setTxdate(bjQuitBean.getTxdate().substring(0,10));
                bjQuit.setRemark(bjQuitBean.getRemark());
                bjQuit.setXwdate(bjQuitBean.getXwdate().substring(0,10));
                bjQuit.setLizhireason(bjQuitBean.getLizhireason());
                bjQuit.setLastworkdate(bjQuitBean.getLastworkdate().substring(0,10));
                bjQuit.setMtjilu(bjQuitBean.getMtjilu());
                bjQuit.setJzdate(bjQuitBean.getJzdate().substring(0,10));

                bjQuit.setImage1(bjQuitBean.getImage1());
                bjQuit.setImage2(bjQuitBean.getImage2());
                bjQuit.setImage3(bjQuitBean.getImage3());
                bjQuitMapper.insert(bjQuit);
                Map<String,String> map1=new HashMap<>();
                map1.put("lizhiid",bjQuit.getId());
                map1.put("sfzh",bjQuitBean.getSfzh());
                list.add(map1);
            }
        }
        return list;
    }

    @Override
    public String addBiandong(BjBdBean bjBdBean) {
        String biandoingid="";
        if(StringUtils.isNotEmpty(bjBdBean.getBiandongid())){
            BjGwbd bjGwbd=bjGwbdMapper.selectById(bjBdBean.getBiandongid());
            bjGwbd.setType(bjBdBean.getType());
            bjGwbd.setOldmain(bjBdBean.getOldmain());
            bjGwbd.setNewbm(bjBdBean.getNewbm());
            bjGwbd.setNewgw(bjBdBean.getNewgw());
            bjGwbd.setNewmain(bjBdBean.getNewmain());
            bjGwbd.setNewjbgz(bjBdBean.getNewjbgz());
            bjGwbd.setJiaotongbutie(bjBdBean.getJiaotongbutie());
            bjGwbd.setTongxunfei(bjBdBean.getTongxunfei());
            bjGwbd.setShbt(bjBdBean.getShbt());
            bjGwbd.setFangbu(bjBdBean.getFangbu());
            bjGwbd.setGljt(bjBdBean.getGljt());
            bjGwbd.setGwgz(bjBdBean.getGwgz());
            bjGwbd.setFdgz(bjBdBean.getFdgz());
            bjGwbd.setYldaijiao(bjBdBean.getYldaijiao());
            bjGwbd.setYldaijiaoje(bjBdBean.getYldaijiaoje());
            bjGwbd.setShiyedaijiao(bjBdBean.getShiyedaijiao());
            bjGwbd.setShiyedaijiaoje(bjBdBean.getShiyedaijiaoje());
            bjGwbd.setYiliaodaijiao(bjBdBean.getYiliaodaijiao());
            bjGwbd.setYiliaodaijiaoje(bjBdBean.getYiliaodaijiaoje());
            bjGwbd.setBgyuanyin(bjBdBean.getBgyuanyin());
            bjGwbd.setSxdt(bjBdBean.getSxdt().substring(0,10));
            bjGwbdMapper.updateById(bjGwbd);
            biandoingid=bjBdBean.getBiandongid();
        }else{
            BjGwbd bjGwbd=new BjGwbd();
            bjGwbd.setName(bjBdBean.getName());
            bjGwbd.setDwname(bjBdBean.getDwname());
            //bjGwbd.setBmname(bjBdBean.getBmname());
            bjGwbd.setBmname(bjBdBean.getGwname());
            bjGwbd.setType(bjBdBean.getType());
            bjGwbd.setZwgz(bjBdBean.getZwgz());
            bjGwbd.setOldmain(bjBdBean.getOldmain());
            bjGwbd.setNewbm(bjBdBean.getNewbm());
            bjGwbd.setNewgw(bjBdBean.getNewgw());
            bjGwbd.setNewmain(bjBdBean.getNewmain());
            bjGwbd.setNewjbgz(bjBdBean.getNewjbgz());
            bjGwbd.setJiaotongbutie(bjBdBean.getJiaotongbutie());
            bjGwbd.setTongxunfei(bjBdBean.getTongxunfei());
            bjGwbd.setShbt(bjBdBean.getShbt());
            bjGwbd.setFangbu(bjBdBean.getFangbu());
            bjGwbd.setGljt(bjBdBean.getGljt());
            bjGwbd.setGwgz(bjBdBean.getGwgz());
            bjGwbd.setFdgz(bjBdBean.getFdgz());
            bjGwbd.setYldaijiao(bjBdBean.getYldaijiao());
            bjGwbd.setYldaijiaoje(bjBdBean.getYldaijiaoje());
            bjGwbd.setShiyedaijiao(bjBdBean.getShiyedaijiao());
            bjGwbd.setShiyedaijiaoje(bjBdBean.getShiyedaijiaoje());
            bjGwbd.setYiliaodaijiao(bjBdBean.getYiliaodaijiao());
            bjGwbd.setYiliaodaijiaoje(bjBdBean.getYiliaodaijiaoje());
            bjGwbd.setBgyuanyin(bjBdBean.getBgyuanyin());
            bjGwbd.setSxdt(bjBdBean.getSxdt().substring(0,10));
            bjGwbd.setYgid(bjBdBean.getId());
            LocalDateTime newDate = LocalDateTime.now();
            //新增4个字段 一个性别一个年龄一个身份证号一个生效时间 性别 1男 2女 年龄的通过borth算
            if(StringUtils.isNotEmpty(bjBdBean.getSex()) || "".equals(bjBdBean.getSex()) || "1".equals(bjBdBean.getSex())){
                bjGwbd.setAge("男");
            }else{
                bjGwbd.setAge("女");
            }
            if (!StringUtils.isNotEmpty(bjBdBean.getBorth()) || !"".equals(bjBdBean.getBorth())){
                bjGwbd.setAge((newDate.getYear()-Integer.parseInt(bjBdBean.getBorth().split("-")[0]))+"");
            }
            //操作一下 生效时间 当前月+1 的01号
            bjGwbd.setIdcard(bjBdBean.getSfzh());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime lastDay = newDate.with(TemporalAdjusters.lastDayOfMonth());
            LocalDateTime afterDay = lastDay.plusDays(1);

            bjGwbd.setBbsxdt(afterDay.format(formatter));
            List<BjYgruzhi> bjYgruzhiList=bjYgruzhiMapper.getListByYgid(bjBdBean.getId());
            if(bjYgruzhiList.isEmpty()==false){
                bjGwbd.setRuzhidt(new SimpleDateFormat("yyyy-MM-dd").format(bjYgruzhiList.get(0).getCreateTime()));
            }else{
                bjGwbd.setRuzhidt(StringUtils.isNotEmpty(bjBdBean.getHtstdt())?bjBdBean.getHtstdt():"");
            }
            bjGwbdMapper.insert(bjGwbd);
            biandoingid=bjGwbd.getId();
        }
        return biandoingid;
    }

    @Override
    public List getStepUserIdByProcInstId(String procInstId) {
        return bjIdcardfkMapper.getStepUserIdByProcInstId(procInstId);
    }

    @Override
    public List<Map<String,String>> getLizhiPersonList(String procInstId){
        ActBusiness actBusiness=actBusinessService.findByProcInstId(procInstId).get(0);
        //获取业务id
        String table_id=actBusiness.getTableId();
        BjIdcardfk bjIdcardfk=getById(table_id);
        String lizhijson=bjIdcardfk.getLizhiJson();
        String ids[]=lizhijson.split(",");
        LambdaQueryWrapper<BjQuit> wrapper = new LambdaQueryWrapper<BjQuit>();
        wrapper.in(BjQuit::getId,ids);
        List<BjQuit> bjQuitList=bjQuitMapper.selectList(wrapper);
        List<Map<String,String>> dataList=new ArrayList<>();
        for(BjQuit bjQuit:bjQuitList){
            Map<String,String> map=new HashMap<>();
            map.put("id",bjQuit.getYgid());
            map.put("name",bjQuit.getName());
            map.put("sex",bjQuit.getSex().equals("1")?"男":"女");
            map.put("sfzh",bjQuit.getSfzh());
            map.put("bumen",bjQuit.getBumen());
            map.put("gwname",bjQuit.getGwname());
            dataList.add(map);
        }
        return dataList;
    }

    @Override
    public Result<?> getLizhiBjQuitPage(Integer pageNo, Integer pageSize) {
        Page<BjQuit> page = new Page<>(pageNo, pageSize);
        QueryWrapper<BjQuit> queryWrapper = new QueryWrapper<>();
        return Result.ok(bjQuitMapper.selectPage(page,queryWrapper));
    }
}
